package com.hunan.api.catv.service.biz.homeRecommend;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hunan.api.catv.common.CacheName;
import com.hunan.api.catv.common.ConstantsMsg;
import com.hunan.api.catv.entity.ServiceResult;
import com.hunan.api.catv.entity.po.HomeRecommend;
import com.hunan.api.catv.entity.vo.HomeRecommendVo;
import com.hunan.api.catv.service.ResultService;
import com.hunan.api.catv.service.impl.homeRecommend.HomeRecommendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

@CacheConfig(cacheNames = CacheName.homeRecommend)
@Service
public class HomeRecommendBiz extends ResultService {

    @Autowired
    HomeRecommendService service;

    public ServiceResult add(HomeRecommend homeRecommend) {
        HomeRecommend h = new HomeRecommend();
        h.setState(1);
        List<HomeRecommendVo> list = service.list(h);
        if (!ObjectUtils.isEmpty(list)) {
            Long nStart = homeRecommend.getStartTime();
            Long nEnd = homeRecommend.getEndTime();
            for (HomeRecommendVo hr : list) {
                Long start = hr.getStartTime();
                Long end = hr.getEndTime();
                if (nStart < end && start < nEnd) {
                    return buildFail(ConstantsMsg.homeRecommend_repeat);
                }
            }
        }
        if (!ObjectUtils.isEmpty(homeRecommend.getLinks())) {
            homeRecommend.setLink(homeRecommend.getLinks());
        }
        ServiceResult addResult = service.add(homeRecommend);
        if (addResult.getStatus()) {
            return buildOK(true);
        }
        return buildFail(ConstantsMsg.admin_add_fail);
    }

    public ServiceResult updateState(Integer rId, Integer state) {
        HomeRecommend h = new HomeRecommend();
        h.setrId(rId);
        h.setState(state);
        ServiceResult updateResult = service.update(h);
        if (updateResult.getStatus()) {
            return buildOK(true);
        }
        return buildFail(ConstantsMsg.admin_edit_fail);
    }

    public ServiceResult update(HomeRecommend homeRecommend) {
        HomeRecommend h = new HomeRecommend();
        h.setState(1);
        List<HomeRecommendVo> list = service.list(h);
        if (!ObjectUtils.isEmpty(list)) {
            Long nStart = homeRecommend.getStartTime();
            Long nEnd = homeRecommend.getEndTime();
            for (HomeRecommendVo hr : list) {
                Long start = hr.getStartTime();
                Long end = hr.getEndTime();
                if (nStart < end && start < nEnd) {
                    return buildFail(ConstantsMsg.homeRecommend_repeat);
                }
            }
        }
        if (!ObjectUtils.isEmpty(homeRecommend.getLinks())) {
            homeRecommend.setLink(homeRecommend.getLinks());
        }
        ServiceResult updateResult = service.update(homeRecommend);
        if (updateResult.getStatus()) {
            return buildOK(true);
        }
        return buildFail(ConstantsMsg.admin_edit_fail);
    }

    public ServiceResult delete(Integer rId) {
        ServiceResult delResult = service.delete(rId);
        if (delResult.getStatus()) {
            return buildOK(true);
        }
        return buildFail(ConstantsMsg.admin_del_fail);
    }

    public PageInfo<HomeRecommendVo> adminPage(Integer pageNum, Integer pageSize, Integer state, Integer type) {
        PageHelper.startPage(pageNum, pageSize);
        HomeRecommend recommend = new HomeRecommend();
        recommend.setType(type);
        recommend.setState(state);
        List<HomeRecommendVo> list = service.list(recommend);
        return new PageInfo<HomeRecommendVo>(list);
    }

    @Cacheable(unless = "#result == null")
    public HomeRecommendVo info() {
        HomeRecommend recommend = new HomeRecommend();
        recommend.setState(1);
        recommend.setNowTime(System.currentTimeMillis());
        List<HomeRecommendVo> list = service.list(recommend);
        if (!ObjectUtils.isEmpty(list)) {
            return list.get(0);
        }
        return new HomeRecommendVo();
    }
}
